﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;

namespace FrozenBubble.Screens
{
    public class InputBoxScreen : GameScreen
    {
        private Texture2D _background;
        private Vector2 _position;
        private Vector2 _textPosition;
        private SpriteFont _font;

        private KeyboardState _oldKeyboard;

        private StringBuilder _sb;

        private string _message;

        public InputBoxScreen(string message)
        {
            _message = message.ToUpperInvariant();
            _sb = new StringBuilder();
        }

        public override void LoadContent()
        {
            _background = Game.Content.Load<Texture2D>("Graphics/Panels/void_panel");

            _position = new Vector2((640 - _background.Width) / 2, (480 - _background.Height) / 2);
            _textPosition = new Vector2(_position.X + 10, _position.Y + 10);
            _font = Game.Content.Load<SpriteFont>("Fonts/font");
        }

        public override void HandleInput(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();

            foreach (Keys key in _oldKeyboard.GetPressedKeys())
            {
                if (keyboard.IsKeyUp(key))
                {
                    // The key has been pressed and released
                    if (key == Keys.Space)
                        _sb.Append(" ");
                    else if (Keys.A <= key && key <= Keys.Z)
                        _sb.Append(key);
                    else if (key == Keys.Back)
                        _sb.Remove(_sb.Length - 1, 1);
                    else if (key == Keys.Enter)
                        ExitScreen();
                }
            }

            _oldKeyboard = keyboard;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Draw(_background, _position, Color.White);
            SpriteBatch.DrawString(_font, _message + "\n\n" + _sb, _textPosition, Color.White);
        }

        public string Text
        {
            get
            {
                return _sb.ToString();
            }
        }

        public static void ShowInputBoxScreen(string title, string description, string defaultText, Action<string> callback)
        {
#if WINDOWS_PHONE
            Guide.BeginShowKeyboardInput(PlayerIndex.One,
                                         title,
                                         description,
                                         defaultText,
                                         (IAsyncResult ar) =>
                                         {
                                            string userName = Guide.EndShowKeyboardInput(ar);
                                            callback(userName);
                                         },
                                         null);
#else
            // Space key has been pressed and released
            InputBoxScreen inputScreen = new InputBoxScreen(title + "\n" + description);
            inputScreen.Closed += new EventHandler((o, e) =>
                                    {
                                        string userName = (sender as InputBoxScreen).Text;
                                        callback(userName);
                                    });
            ScreenManager.AddScreen(inputScreen);
#endif
        }
    }
}
